home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / info / lispref.info-8.z / lispref.info-8
Encoding:
GNU Info File  |  1998-05-21  |  48.4 KB  |  1,262 lines

  1. This is Info file ../../info/lispref.info, produced by Makeinfo version
  2. 1.68 from the input file lispref.texi.
  3.  
  4.    Edition History:
  5.  
  6.    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
  7. Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
  8. Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
  9. XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
  10. GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
  11. Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
  12. Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
  13. Reference Manual (for 19.15 and 20.1, 20.2) v3.2, April, May 1997
  14.  
  15.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
  16. Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
  17. Copyright (C) 1995, 1996 Ben Wing.
  18.  
  19.    Permission is granted to make and distribute verbatim copies of this
  20. manual provided the copyright notice and this permission notice are
  21. preserved on all copies.
  22.  
  23.    Permission is granted to copy and distribute modified versions of
  24. this manual under the conditions for verbatim copying, provided that the
  25. entire resulting derived work is distributed under the terms of a
  26. permission notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that this permission notice may be stated in a
  31. translation approved by the Foundation.
  32.  
  33.    Permission is granted to copy and distribute modified versions of
  34. this manual under the conditions for verbatim copying, provided also
  35. that the section entitled "GNU General Public License" is included
  36. exactly as in the original, and provided that the entire resulting
  37. derived work is distributed under the terms of a permission notice
  38. identical to this one.
  39.  
  40.    Permission is granted to copy and distribute translations of this
  41. manual into another language, under the above conditions for modified
  42. versions, except that the section entitled "GNU General Public License"
  43. may be included in a translation approved by the Free Software
  44. Foundation instead of in the original English.
  45.  
  46. 
  47. File: lispref.info,  Node: Symbol Forms,  Next: Classifying Lists,  Prev: Self-Evaluating Forms,  Up: Forms
  48.  
  49. Symbol Forms
  50. ------------
  51.  
  52.    When a symbol is evaluated, it is treated as a variable.  The result
  53. is the variable's value, if it has one.  If it has none (if its value
  54. cell is void), an error is signaled.  For more information on the use of
  55. variables, see *Note Variables::.
  56.  
  57.    In the following example, we set the value of a symbol with `setq'.
  58. Then we evaluate the symbol, and get back the value that `setq' stored.
  59.  
  60.      (setq a 123)
  61.           => 123
  62.      (eval 'a)
  63.           => 123
  64.      a
  65.           => 123
  66.  
  67.    The symbols `nil' and `t' are treated specially, so that the value
  68. of `nil' is always `nil', and the value of `t' is always `t'; you
  69. cannot set or bind them to any other values.  Thus, these two symbols
  70. act like self-evaluating forms, even though `eval' treats them like any
  71. other symbol.
  72.  
  73. 
  74. File: lispref.info,  Node: Classifying Lists,  Next: Function Indirection,  Prev: Symbol Forms,  Up: Forms
  75.  
  76. Classification of List Forms
  77. ----------------------------
  78.  
  79.    A form that is a nonempty list is either a function call, a macro
  80. call, or a special form, according to its first element.  These three
  81. kinds of forms are evaluated in different ways, described below.  The
  82. remaining list elements constitute the "arguments" for the function,
  83. macro, or special form.
  84.  
  85.    The first step in evaluating a nonempty list is to examine its first
  86. element.  This element alone determines what kind of form the list is
  87. and how the rest of the list is to be processed.  The first element is
  88. *not* evaluated, as it would be in some Lisp dialects such as Scheme.
  89.  
  90. 
  91. File: lispref.info,  Node: Function Indirection,  Next: Function Forms,  Prev: Classifying Lists,  Up: Forms
  92.  
  93. Symbol Function Indirection
  94. ---------------------------
  95.  
  96.    If the first element of the list is a symbol then evaluation examines
  97. the symbol's function cell, and uses its contents instead of the
  98. original symbol.  If the contents are another symbol, this process,
  99. called "symbol function indirection", is repeated until it obtains a
  100. non-symbol.  *Note Function Names::, for more information about using a
  101. symbol as a name for a function stored in the function cell of the
  102. symbol.
  103.  
  104.    One possible consequence of this process is an infinite loop, in the
  105. event that a symbol's function cell refers to the same symbol.  Or a
  106. symbol may have a void function cell, in which case the subroutine
  107. `symbol-function' signals a `void-function' error.  But if neither of
  108. these things happens, we eventually obtain a non-symbol, which ought to
  109. be a function or other suitable object.
  110.  
  111.    More precisely, we should now have a Lisp function (a lambda
  112. expression), a byte-code function, a primitive function, a Lisp macro, a
  113. special form, or an autoload object.  Each of these types is a case
  114. described in one of the following sections.  If the object is not one of
  115. these types, the error `invalid-function' is signaled.
  116.  
  117.    The following example illustrates the symbol indirection process.  We
  118. use `fset' to set the function cell of a symbol and `symbol-function'
  119. to get the function cell contents (*note Function Cells::.).
  120. Specifically, we store the symbol `car' into the function cell of
  121. `first', and the symbol `first' into the function cell of `erste'.
  122.  
  123.      ;; Build this function cell linkage:
  124.      ;;   -------------       -----        -------        -------
  125.      ;;  | #<subr car> | <-- | car |  <-- | first |  <-- | erste |
  126.      ;;   -------------       -----        -------        -------
  127.  
  128.      (symbol-function 'car)
  129.           => #<subr car>
  130.  
  131.      (fset 'first 'car)
  132.           => car
  133.  
  134.      (fset 'erste 'first)
  135.           => first
  136.  
  137.      (erste '(1 2 3))   ; Call the function referenced by `erste'.
  138.           => 1
  139.  
  140.    By contrast, the following example calls a function without any
  141. symbol function indirection, because the first element is an anonymous
  142. Lisp function, not a symbol.
  143.  
  144.      ((lambda (arg) (erste arg))
  145.       '(1 2 3))
  146.           => 1
  147.  
  148. Executing the function itself evaluates its body; this does involve
  149. symbol function indirection when calling `erste'.
  150.  
  151.    The built-in function `indirect-function' provides an easy way to
  152. perform symbol function indirection explicitly.
  153.  
  154.  - Function: indirect-function FUNCTION
  155.      This function returns the meaning of FUNCTION as a function.  If
  156.      FUNCTION is a symbol, then it finds FUNCTION's function definition
  157.      and starts over with that value.  If FUNCTION is not a symbol,
  158.      then it returns FUNCTION itself.
  159.  
  160.      Here is how you could define `indirect-function' in Lisp:
  161.  
  162.           (defun indirect-function (function)
  163.             (if (symbolp function)
  164.                 (indirect-function (symbol-function function))
  165.               function))
  166.  
  167. 
  168. File: lispref.info,  Node: Function Forms,  Next: Macro Forms,  Prev: Function Indirection,  Up: Forms
  169.  
  170. Evaluation of Function Forms
  171. ----------------------------
  172.  
  173.    If the first element of a list being evaluated is a Lisp function
  174. object, byte-code object or primitive function object, then that list is
  175. a "function call".  For example, here is a call to the function `+':
  176.  
  177.      (+ 1 x)
  178.  
  179.    The first step in evaluating a function call is to evaluate the
  180. remaining elements of the list from left to right.  The results are the
  181. actual argument values, one value for each list element.  The next step
  182. is to call the function with this list of arguments, effectively using
  183. the function `apply' (*note Calling Functions::.).  If the function is
  184. written in Lisp, the arguments are used to bind the argument variables
  185. of the function (*note Lambda Expressions::.); then the forms in the
  186. function body are evaluated in order, and the value of the last body
  187. form becomes the value of the function call.
  188.  
  189. 
  190. File: lispref.info,  Node: Macro Forms,  Next: Special Forms,  Prev: Function Forms,  Up: Forms
  191.  
  192. Lisp Macro Evaluation
  193. ---------------------
  194.  
  195.    If the first element of a list being evaluated is a macro object,
  196. then the list is a "macro call".  When a macro call is evaluated, the
  197. elements of the rest of the list are *not* initially evaluated.
  198. Instead, these elements themselves are used as the arguments of the
  199. macro.  The macro definition computes a replacement form, called the
  200. "expansion" of the macro, to be evaluated in place of the original
  201. form.  The expansion may be any sort of form: a self-evaluating
  202. constant, a symbol, or a list.  If the expansion is itself a macro call,
  203. this process of expansion repeats until some other sort of form results.
  204.  
  205.    Ordinary evaluation of a macro call finishes by evaluating the
  206. expansion.  However, the macro expansion is not necessarily evaluated
  207. right away, or at all, because other programs also expand macro calls,
  208. and they may or may not evaluate the expansions.
  209.  
  210.    Normally, the argument expressions are not evaluated as part of
  211. computing the macro expansion, but instead appear as part of the
  212. expansion, so they are computed when the expansion is computed.
  213.  
  214.    For example, given a macro defined as follows:
  215.  
  216.      (defmacro cadr (x)
  217.        (list 'car (list 'cdr x)))
  218.  
  219. an expression such as `(cadr (assq 'handler list))' is a macro call,
  220. and its expansion is:
  221.  
  222.      (car (cdr (assq 'handler list)))
  223.  
  224. Note that the argument `(assq 'handler list)' appears in the expansion.
  225.  
  226.    *Note Macros::, for a complete description of XEmacs Lisp macros.
  227.  
  228. 
  229. File: lispref.info,  Node: Special Forms,  Next: Autoloading,  Prev: Macro Forms,  Up: Forms
  230.  
  231. Special Forms
  232. -------------
  233.  
  234.    A "special form" is a primitive function specially marked so that
  235. its arguments are not all evaluated.  Most special forms define control
  236. structures or perform variable bindings--things which functions cannot
  237. do.
  238.  
  239.    Each special form has its own rules for which arguments are evaluated
  240. and which are used without evaluation.  Whether a particular argument is
  241. evaluated may depend on the results of evaluating other arguments.
  242.  
  243.    Here is a list, in alphabetical order, of all of the special forms in
  244. XEmacs Lisp with a reference to where each is described.
  245.  
  246. `and'
  247.      *note Combining Conditions::.
  248.  
  249. `catch'
  250.      *note Catch and Throw::.
  251.  
  252. `cond'
  253.      *note Conditionals::.
  254.  
  255. `condition-case'
  256.      *note Handling Errors::.
  257.  
  258. `defconst'
  259.      *note Defining Variables::.
  260.  
  261. `defmacro'
  262.      *note Defining Macros::.
  263.  
  264. `defun'
  265.      *note Defining Functions::.
  266.  
  267. `defvar'
  268.      *note Defining Variables::.
  269.  
  270. `function'
  271.      *note Anonymous Functions::.
  272.  
  273. `if'
  274.      *note Conditionals::.
  275.  
  276. `interactive'
  277.      *note Interactive Call::.
  278.  
  279. `let'
  280. `let*'
  281.      *note Local Variables::.
  282.  
  283. `or'
  284.      *note Combining Conditions::.
  285.  
  286. `prog1'
  287. `prog2'
  288. `progn'
  289.      *note Sequencing::.
  290.  
  291. `quote'
  292.      *note Quoting::.
  293.  
  294. `save-current-buffer'
  295.      *note Excursions::.
  296.  
  297. `save-excursion'
  298.      *note Excursions::.
  299.  
  300. `save-restriction'
  301.      *note Narrowing::.
  302.  
  303. `save-selected-window'
  304.      *note Excursions::.
  305.  
  306. `save-window-excursion'
  307.      *note Window Configurations::.
  308.  
  309. `setq'
  310.      *note Setting Variables::.
  311.  
  312. `setq-default'
  313.      *note Creating Buffer-Local::.
  314.  
  315. `unwind-protect'
  316.      *note Nonlocal Exits::.
  317.  
  318. `while'
  319.      *note Iteration::.
  320.  
  321. `with-output-to-temp-buffer'
  322.      *note Temporary Displays::.
  323.  
  324.      Common Lisp note: here are some comparisons of special forms in
  325.      XEmacs Lisp and Common Lisp.  `setq', `if', and `catch' are
  326.      special forms in both XEmacs Lisp and Common Lisp.  `defun' is a
  327.      special form in XEmacs Lisp, but a macro in Common Lisp.
  328.      `save-excursion' is a special form in XEmacs Lisp, but doesn't
  329.      exist in Common Lisp.  `throw' is a special form in Common Lisp
  330.      (because it must be able to throw multiple values), but it is a
  331.      function in XEmacs Lisp (which doesn't have multiple values).
  332.  
  333. 
  334. File: lispref.info,  Node: Autoloading,  Prev: Special Forms,  Up: Forms
  335.  
  336. Autoloading
  337. -----------
  338.  
  339.    The "autoload" feature allows you to call a function or macro whose
  340. function definition has not yet been loaded into XEmacs.  It specifies
  341. which file contains the definition.  When an autoload object appears as
  342. a symbol's function definition, calling that symbol as a function
  343. automatically loads the specified file; then it calls the real
  344. definition loaded from that file.  *Note Autoload::.
  345.  
  346. 
  347. File: lispref.info,  Node: Quoting,  Prev: Forms,  Up: Evaluation
  348.  
  349. Quoting
  350. =======
  351.  
  352.    The special form `quote' returns its single argument, as written,
  353. without evaluating it.  This provides a way to include constant symbols
  354. and lists, which are not self-evaluating objects, in a program.  (It is
  355. not necessary to quote self-evaluating objects such as numbers, strings,
  356. and vectors.)
  357.  
  358.  - Special Form: quote OBJECT
  359.      This special form returns OBJECT, without evaluating it.
  360.  
  361.    Because `quote' is used so often in programs, Lisp provides a
  362. convenient read syntax for it.  An apostrophe character (`'') followed
  363. by a Lisp object (in read syntax) expands to a list whose first element
  364. is `quote', and whose second element is the object.  Thus, the read
  365. syntax `'x' is an abbreviation for `(quote x)'.
  366.  
  367.    Here are some examples of expressions that use `quote':
  368.  
  369.      (quote (+ 1 2))
  370.           => (+ 1 2)
  371.      (quote foo)
  372.           => foo
  373.      'foo
  374.           => foo
  375.      ''foo
  376.           => (quote foo)
  377.      '(quote foo)
  378.           => (quote foo)
  379.      ['foo]
  380.           => [(quote foo)]
  381.  
  382.    Other quoting constructs include `function' (*note Anonymous
  383. Functions::.), which causes an anonymous lambda expression written in
  384. Lisp to be compiled, and ``' (*note Backquote::.), which is used to
  385. quote only part of a list, while computing and substituting other parts.
  386.  
  387. 
  388. File: lispref.info,  Node: Control Structures,  Next: Variables,  Prev: Evaluation,  Up: Top
  389.  
  390. Control Structures
  391. ******************
  392.  
  393.    A Lisp program consists of expressions or "forms" (*note Forms::.).
  394. We control the order of execution of the forms by enclosing them in
  395. "control structures".  Control structures are special forms which
  396. control when, whether, or how many times to execute the forms they
  397. contain.
  398.  
  399.    The simplest order of execution is sequential execution: first form
  400. A, then form B, and so on.  This is what happens when you write several
  401. forms in succession in the body of a function, or at top level in a
  402. file of Lisp code--the forms are executed in the order written.  We
  403. call this "textual order".  For example, if a function body consists of
  404. two forms A and B, evaluation of the function evaluates first A and
  405. then B, and the function's value is the value of B.
  406.  
  407.    Explicit control structures make possible an order of execution other
  408. than sequential.
  409.  
  410.    XEmacs Lisp provides several kinds of control structure, including
  411. other varieties of sequencing, conditionals, iteration, and (controlled)
  412. jumps--all discussed below.  The built-in control structures are
  413. special forms since their subforms are not necessarily evaluated or not
  414. evaluated sequentially.  You can use macros to define your own control
  415. structure constructs (*note Macros::.).
  416.  
  417. * Menu:
  418.  
  419. * Sequencing::             Evaluation in textual order.
  420. * Conditionals::           `if', `cond'.
  421. * Combining Conditions::   `and', `or', `not'.
  422. * Iteration::              `while' loops.
  423. * Nonlocal Exits::         Jumping out of a sequence.
  424.  
  425. 
  426. File: lispref.info,  Node: Sequencing,  Next: Conditionals,  Up: Control Structures
  427.  
  428. Sequencing
  429. ==========
  430.  
  431.    Evaluating forms in the order they appear is the most common way
  432. control passes from one form to another.  In some contexts, such as in a
  433. function body, this happens automatically.  Elsewhere you must use a
  434. control structure construct to do this: `progn', the simplest control
  435. construct of Lisp.
  436.  
  437.    A `progn' special form looks like this:
  438.  
  439.      (progn A B C ...)
  440.  
  441. and it says to execute the forms A, B, C and so on, in that order.
  442. These forms are called the body of the `progn' form.  The value of the
  443. last form in the body becomes the value of the entire `progn'.
  444.  
  445.    In the early days of Lisp, `progn' was the only way to execute two
  446. or more forms in succession and use the value of the last of them.  But
  447. programmers found they often needed to use a `progn' in the body of a
  448. function, where (at that time) only one form was allowed.  So the body
  449. of a function was made into an "implicit `progn'": several forms are
  450. allowed just as in the body of an actual `progn'.  Many other control
  451. structures likewise contain an implicit `progn'.  As a result, `progn'
  452. is not used as often as it used to be.  It is needed now most often
  453. inside an `unwind-protect', `and', `or', or in the THEN-part of an `if'.
  454.  
  455.  - Special Form: progn FORMS...
  456.      This special form evaluates all of the FORMS, in textual order,
  457.      returning the result of the final form.
  458.  
  459.           (progn (print "The first form")
  460.                  (print "The second form")
  461.                  (print "The third form"))
  462.                -| "The first form"
  463.                -| "The second form"
  464.                -| "The third form"
  465.           => "The third form"
  466.  
  467.    Two other control constructs likewise evaluate a series of forms but
  468. return a different value:
  469.  
  470.  - Special Form: prog1 FORM1 FORMS...
  471.      This special form evaluates FORM1 and all of the FORMS, in textual
  472.      order, returning the result of FORM1.
  473.  
  474.           (prog1 (print "The first form")
  475.                  (print "The second form")
  476.                  (print "The third form"))
  477.                -| "The first form"
  478.                -| "The second form"
  479.                -| "The third form"
  480.           => "The first form"
  481.  
  482.      Here is a way to remove the first element from a list in the
  483.      variable `x', then return the value of that former element:
  484.  
  485.           (prog1 (car x) (setq x (cdr x)))
  486.  
  487.  - Special Form: prog2 FORM1 FORM2 FORMS...
  488.      This special form evaluates FORM1, FORM2, and all of the following
  489.      FORMS, in textual order, returning the result of FORM2.
  490.  
  491.           (prog2 (print "The first form")
  492.                  (print "The second form")
  493.                  (print "The third form"))
  494.                -| "The first form"
  495.                -| "The second form"
  496.                -| "The third form"
  497.           => "The second form"
  498.  
  499. 
  500. File: lispref.info,  Node: Conditionals,  Next: Combining Conditions,  Prev: Sequencing,  Up: Control Structures
  501.  
  502. Conditionals
  503. ============
  504.  
  505.    Conditional control structures choose among alternatives.  XEmacs
  506. Lisp has two conditional forms: `if', which is much the same as in other
  507. languages, and `cond', which is a generalized case statement.
  508.  
  509.  - Special Form: if CONDITION THEN-FORM ELSE-FORMS...
  510.      `if' chooses between the THEN-FORM and the ELSE-FORMS based on the
  511.      value of CONDITION.  If the evaluated CONDITION is non-`nil',
  512.      THEN-FORM is evaluated and the result returned.  Otherwise, the
  513.      ELSE-FORMS are evaluated in textual order, and the value of the
  514.      last one is returned.  (The ELSE part of `if' is an example of an
  515.      implicit `progn'.  *Note Sequencing::.)
  516.  
  517.      If CONDITION has the value `nil', and no ELSE-FORMS are given,
  518.      `if' returns `nil'.
  519.  
  520.      `if' is a special form because the branch that is not selected is
  521.      never evaluated--it is ignored.  Thus, in the example below,
  522.      `true' is not printed because `print' is never called.
  523.  
  524.           (if nil
  525.               (print 'true)
  526.             'very-false)
  527.           => very-false
  528.  
  529.  - Special Form: cond CLAUSE...
  530.      `cond' chooses among an arbitrary number of alternatives.  Each
  531.      CLAUSE in the `cond' must be a list.  The CAR of this list is the
  532.      CONDITION; the remaining elements, if any, the BODY-FORMS.  Thus,
  533.      a clause looks like this:
  534.  
  535.           (CONDITION BODY-FORMS...)
  536.  
  537.      `cond' tries the clauses in textual order, by evaluating the
  538.      CONDITION of each clause.  If the value of CONDITION is non-`nil',
  539.      the clause "succeeds"; then `cond' evaluates its BODY-FORMS, and
  540.      the value of the last of BODY-FORMS becomes the value of the
  541.      `cond'.  The remaining clauses are ignored.
  542.  
  543.      If the value of CONDITION is `nil', the clause "fails", so the
  544.      `cond' moves on to the following clause, trying its CONDITION.
  545.  
  546.      If every CONDITION evaluates to `nil', so that every clause fails,
  547.      `cond' returns `nil'.
  548.  
  549.      A clause may also look like this:
  550.  
  551.           (CONDITION)
  552.  
  553.      Then, if CONDITION is non-`nil' when tested, the value of
  554.      CONDITION becomes the value of the `cond' form.
  555.  
  556.      The following example has four clauses, which test for the cases
  557.      where the value of `x' is a number, string, buffer and symbol,
  558.      respectively:
  559.  
  560.           (cond ((numberp x) x)
  561.                 ((stringp x) x)
  562.                 ((bufferp x)
  563.                  (setq temporary-hack x) ; multiple body-forms
  564.                  (buffer-name x))        ; in one clause
  565.                 ((symbolp x) (symbol-value x)))
  566.  
  567.      Often we want to execute the last clause whenever none of the
  568.      previous clauses was successful.  To do this, we use `t' as the
  569.      CONDITION of the last clause, like this: `(t BODY-FORMS)'.  The
  570.      form `t' evaluates to `t', which is never `nil', so this clause
  571.      never fails, provided the `cond' gets to it at all.
  572.  
  573.      For example,
  574.  
  575.           (cond ((eq a 'hack) 'foo)
  576.                 (t "default"))
  577.           => "default"
  578.  
  579.      This expression is a `cond' which returns `foo' if the value of
  580.      `a' is 1, and returns the string `"default"' otherwise.
  581.  
  582.    Any conditional construct can be expressed with `cond' or with `if'.
  583. Therefore, the choice between them is a matter of style.  For example:
  584.  
  585.      (if A B C)
  586.      ==
  587.      (cond (A B) (t C))
  588.  
  589. 
  590. File: lispref.info,  Node: Combining Conditions,  Next: Iteration,  Prev: Conditionals,  Up: Control Structures
  591.  
  592. Constructs for Combining Conditions
  593. ===================================
  594.  
  595.    This section describes three constructs that are often used together
  596. with `if' and `cond' to express complicated conditions.  The constructs
  597. `and' and `or' can also be used individually as kinds of multiple
  598. conditional constructs.
  599.  
  600.  - Function: not CONDITION
  601.      This function tests for the falsehood of CONDITION.  It returns
  602.      `t' if CONDITION is `nil', and `nil' otherwise.  The function
  603.      `not' is identical to `null', and we recommend using the name
  604.      `null' if you are testing for an empty list.
  605.  
  606.  - Special Form: and CONDITIONS...
  607.      The `and' special form tests whether all the CONDITIONS are true.
  608.      It works by evaluating the CONDITIONS one by one in the order
  609.      written.
  610.  
  611.      If any of the CONDITIONS evaluates to `nil', then the result of
  612.      the `and' must be `nil' regardless of the remaining CONDITIONS; so
  613.      `and' returns right away, ignoring the remaining CONDITIONS.
  614.  
  615.      If all the CONDITIONS turn out non-`nil', then the value of the
  616.      last of them becomes the value of the `and' form.
  617.  
  618.      Here is an example.  The first condition returns the integer 1,
  619.      which is not `nil'.  Similarly, the second condition returns the
  620.      integer 2, which is not `nil'.  The third condition is `nil', so
  621.      the remaining condition is never evaluated.
  622.  
  623.           (and (print 1) (print 2) nil (print 3))
  624.                -| 1
  625.                -| 2
  626.           => nil
  627.  
  628.      Here is a more realistic example of using `and':
  629.  
  630.           (if (and (consp foo) (eq (car foo) 'x))
  631.               (message "foo is a list starting with x"))
  632.  
  633.      Note that `(car foo)' is not executed if `(consp foo)' returns
  634.      `nil', thus avoiding an error.
  635.  
  636.      `and' can be expressed in terms of either `if' or `cond'.  For
  637.      example:
  638.  
  639.           (and ARG1 ARG2 ARG3)
  640.           ==
  641.           (if ARG1 (if ARG2 ARG3))
  642.           ==
  643.           (cond (ARG1 (cond (ARG2 ARG3))))
  644.  
  645.  - Special Form: or CONDITIONS...
  646.      The `or' special form tests whether at least one of the CONDITIONS
  647.      is true.  It works by evaluating all the CONDITIONS one by one in
  648.      the order written.
  649.  
  650.      If any of the CONDITIONS evaluates to a non-`nil' value, then the
  651.      result of the `or' must be non-`nil'; so `or' returns right away,
  652.      ignoring the remaining CONDITIONS.  The value it returns is the
  653.      non-`nil' value of the condition just evaluated.
  654.  
  655.      If all the CONDITIONS turn out `nil', then the `or' expression
  656.      returns `nil'.
  657.  
  658.      For example, this expression tests whether `x' is either 0 or
  659.      `nil':
  660.  
  661.           (or (eq x nil) (eq x 0))
  662.  
  663.      Like the `and' construct, `or' can be written in terms of `cond'.
  664.      For example:
  665.  
  666.           (or ARG1 ARG2 ARG3)
  667.           ==
  668.           (cond (ARG1)
  669.                 (ARG2)
  670.                 (ARG3))
  671.  
  672.      You could almost write `or' in terms of `if', but not quite:
  673.  
  674.           (if ARG1 ARG1
  675.             (if ARG2 ARG2
  676.               ARG3))
  677.  
  678.      This is not completely equivalent because it can evaluate ARG1 or
  679.      ARG2 twice.  By contrast, `(or ARG1 ARG2 ARG3)' never evaluates
  680.      any argument more than once.
  681.  
  682. 
  683. File: lispref.info,  Node: Iteration,  Next: Nonlocal Exits,  Prev: Combining Conditions,  Up: Control Structures
  684.  
  685. Iteration
  686. =========
  687.  
  688.    Iteration means executing part of a program repetitively.  For
  689. example, you might want to repeat some computation once for each element
  690. of a list, or once for each integer from 0 to N.  You can do this in
  691. XEmacs Lisp with the special form `while':
  692.  
  693.  - Special Form: while CONDITION FORMS...
  694.      `while' first evaluates CONDITION.  If the result is non-`nil', it
  695.      evaluates FORMS in textual order.  Then it reevaluates CONDITION,
  696.      and if the result is non-`nil', it evaluates FORMS again.  This
  697.      process repeats until CONDITION evaluates to `nil'.
  698.  
  699.      There is no limit on the number of iterations that may occur.  The
  700.      loop will continue until either CONDITION evaluates to `nil' or
  701.      until an error or `throw' jumps out of it (*note Nonlocal
  702.      Exits::.).
  703.  
  704.      The value of a `while' form is always `nil'.
  705.  
  706.           (setq num 0)
  707.                => 0
  708.           (while (< num 4)
  709.             (princ (format "Iteration %d." num))
  710.             (setq num (1+ num)))
  711.                -| Iteration 0.
  712.                -| Iteration 1.
  713.                -| Iteration 2.
  714.                -| Iteration 3.
  715.                => nil
  716.  
  717.      If you would like to execute something on each iteration before the
  718.      end-test, put it together with the end-test in a `progn' as the
  719.      first argument of `while', as shown here:
  720.  
  721.           (while (progn
  722.                    (forward-line 1)
  723.                    (not (looking-at "^$"))))
  724.  
  725.      This moves forward one line and continues moving by lines until it
  726.      reaches an empty.  It is unusual in that the `while' has no body,
  727.      just the end test (which also does the real work of moving point).
  728.  
  729. 
  730. File: lispref.info,  Node: Nonlocal Exits,  Prev: Iteration,  Up: Control Structures
  731.  
  732. Nonlocal Exits
  733. ==============
  734.  
  735.    A "nonlocal exit" is a transfer of control from one point in a
  736. program to another remote point.  Nonlocal exits can occur in XEmacs
  737. Lisp as a result of errors; you can also use them under explicit
  738. control.  Nonlocal exits unbind all variable bindings made by the
  739. constructs being exited.
  740.  
  741. * Menu:
  742.  
  743. * Catch and Throw::     Nonlocal exits for the program's own purposes.
  744. * Examples of Catch::   Showing how such nonlocal exits can be written.
  745. * Errors::              How errors are signaled and handled.
  746. * Cleanups::            Arranging to run a cleanup form if an error happens.
  747.  
  748. 
  749. File: lispref.info,  Node: Catch and Throw,  Next: Examples of Catch,  Up: Nonlocal Exits
  750.  
  751. Explicit Nonlocal Exits: `catch' and `throw'
  752. --------------------------------------------
  753.  
  754.    Most control constructs affect only the flow of control within the
  755. construct itself.  The function `throw' is the exception to this rule
  756. of normal program execution: it performs a nonlocal exit on request.
  757. (There are other exceptions, but they are for error handling only.)
  758. `throw' is used inside a `catch', and jumps back to that `catch'.  For
  759. example:
  760.  
  761.      (catch 'foo
  762.        (progn
  763.          ...
  764.          (throw 'foo t)
  765.          ...))
  766.  
  767. The `throw' transfers control straight back to the corresponding
  768. `catch', which returns immediately.  The code following the `throw' is
  769. not executed.  The second argument of `throw' is used as the return
  770. value of the `catch'.
  771.  
  772.    The `throw' and the `catch' are matched through the first argument:
  773. `throw' searches for a `catch' whose first argument is `eq' to the one
  774. specified.  Thus, in the above example, the `throw' specifies `foo',
  775. and the `catch' specifies the same symbol, so that `catch' is
  776. applicable.  If there is more than one applicable `catch', the
  777. innermost one takes precedence.
  778.  
  779.    Executing `throw' exits all Lisp constructs up to the matching
  780. `catch', including function calls.  When binding constructs such as
  781. `let' or function calls are exited in this way, the bindings are
  782. unbound, just as they are when these constructs exit normally (*note
  783. Local Variables::.).  Likewise, `throw' restores the buffer and
  784. position saved by `save-excursion' (*note Excursions::.), and the
  785. narrowing status saved by `save-restriction' and the window selection
  786. saved by `save-window-excursion' (*note Window Configurations::.).  It
  787. also runs any cleanups established with the `unwind-protect' special
  788. form when it exits that form (*note Cleanups::.).
  789.  
  790.    The `throw' need not appear lexically within the `catch' that it
  791. jumps to.  It can equally well be called from another function called
  792. within the `catch'.  As long as the `throw' takes place chronologically
  793. after entry to the `catch', and chronologically before exit from it, it
  794. has access to that `catch'.  This is why `throw' can be used in
  795. commands such as `exit-recursive-edit' that throw back to the editor
  796. command loop (*note Recursive Editing::.).
  797.  
  798.      Common Lisp note: Most other versions of Lisp, including Common
  799.      Lisp, have several ways of transferring control nonsequentially:
  800.      `return', `return-from', and `go', for example.  XEmacs Lisp has
  801.      only `throw'.
  802.  
  803.  - Special Form: catch TAG BODY...
  804.      `catch' establishes a return point for the `throw' function.  The
  805.      return point is distinguished from other such return points by TAG,
  806.      which may be any Lisp object.  The argument TAG is evaluated
  807.      normally before the return point is established.
  808.  
  809.      With the return point in effect, `catch' evaluates the forms of the
  810.      BODY in textual order.  If the forms execute normally, without
  811.      error or nonlocal exit, the value of the last body form is
  812.      returned from the `catch'.
  813.  
  814.      If a `throw' is done within BODY specifying the same value TAG,
  815.      the `catch' exits immediately; the value it returns is whatever
  816.      was specified as the second argument of `throw'.
  817.  
  818.  - Function: throw TAG VALUE
  819.      The purpose of `throw' is to return from a return point previously
  820.      established with `catch'.  The argument TAG is used to choose
  821.      among the various existing return points; it must be `eq' to the
  822.      value specified in the `catch'.  If multiple return points match
  823.      TAG, the innermost one is used.
  824.  
  825.      The argument VALUE is used as the value to return from that
  826.      `catch'.
  827.  
  828.      If no return point is in effect with tag TAG, then a `no-catch'
  829.      error is signaled with data `(TAG VALUE)'.
  830.  
  831. 
  832. File: lispref.info,  Node: Examples of Catch,  Next: Errors,  Prev: Catch and Throw,  Up: Nonlocal Exits
  833.  
  834. Examples of `catch' and `throw'
  835. -------------------------------
  836.  
  837.    One way to use `catch' and `throw' is to exit from a doubly nested
  838. loop.  (In most languages, this would be done with a "go to".)  Here we
  839. compute `(foo I J)' for I and J varying from 0 to 9:
  840.  
  841.      (defun search-foo ()
  842.        (catch 'loop
  843.          (let ((i 0))
  844.            (while (< i 10)
  845.              (let ((j 0))
  846.                (while (< j 10)
  847.                  (if (foo i j)
  848.                      (throw 'loop (list i j)))
  849.                  (setq j (1+ j))))
  850.              (setq i (1+ i))))))
  851.  
  852. If `foo' ever returns non-`nil', we stop immediately and return a list
  853. of I and J.  If `foo' always returns `nil', the `catch' returns
  854. normally, and the value is `nil', since that is the result of the
  855. `while'.
  856.  
  857.    Here are two tricky examples, slightly different, showing two return
  858. points at once.  First, two return points with the same tag, `hack':
  859.  
  860.      (defun catch2 (tag)
  861.        (catch tag
  862.          (throw 'hack 'yes)))
  863.      => catch2
  864.      
  865.      (catch 'hack
  866.        (print (catch2 'hack))
  867.        'no)
  868.      -| yes
  869.      => no
  870.  
  871. Since both return points have tags that match the `throw', it goes to
  872. the inner one, the one established in `catch2'.  Therefore, `catch2'
  873. returns normally with value `yes', and this value is printed.  Finally
  874. the second body form in the outer `catch', which is `'no', is evaluated
  875. and returned from the outer `catch'.
  876.  
  877.    Now let's change the argument given to `catch2':
  878.  
  879.      (defun catch2 (tag)
  880.        (catch tag
  881.          (throw 'hack 'yes)))
  882.      => catch2
  883.      
  884.      (catch 'hack
  885.        (print (catch2 'quux))
  886.        'no)
  887.      => yes
  888.  
  889. We still have two return points, but this time only the outer one has
  890. the tag `hack'; the inner one has the tag `quux' instead.  Therefore,
  891. `throw' makes the outer `catch' return the value `yes'.  The function
  892. `print' is never called, and the body-form `'no' is never evaluated.
  893.  
  894. 
  895. File: lispref.info,  Node: Errors,  Next: Cleanups,  Prev: Examples of Catch,  Up: Nonlocal Exits
  896.  
  897. Errors
  898. ------
  899.  
  900.    When XEmacs Lisp attempts to evaluate a form that, for some reason,
  901. cannot be evaluated, it "signals" an "error".
  902.  
  903.    When an error is signaled, XEmacs's default reaction is to print an
  904. error message and terminate execution of the current command.  This is
  905. the right thing to do in most cases, such as if you type `C-f' at the
  906. end of the buffer.
  907.  
  908.    In complicated programs, simple termination may not be what you want.
  909. For example, the program may have made temporary changes in data
  910. structures, or created temporary buffers that should be deleted before
  911. the program is finished.  In such cases, you would use `unwind-protect'
  912. to establish "cleanup expressions" to be evaluated in case of error.
  913. (*Note Cleanups::.)  Occasionally, you may wish the program to continue
  914. execution despite an error in a subroutine.  In these cases, you would
  915. use `condition-case' to establish "error handlers" to recover control
  916. in case of error.
  917.  
  918.    Resist the temptation to use error handling to transfer control from
  919. one part of the program to another; use `catch' and `throw' instead.
  920. *Note Catch and Throw::.
  921.  
  922. * Menu:
  923.  
  924. * Signaling Errors::      How to report an error.
  925. * Processing of Errors::  What XEmacs does when you report an error.
  926. * Handling Errors::       How you can trap errors and continue execution.
  927. * Error Symbols::         How errors are classified for trapping them.
  928.  
  929. 
  930. File: lispref.info,  Node: Signaling Errors,  Next: Processing of Errors,  Up: Errors
  931.  
  932. How to Signal an Error
  933. ......................
  934.  
  935.    Most errors are signaled "automatically" within Lisp primitives
  936. which you call for other purposes, such as if you try to take the CAR
  937. of an integer or move forward a character at the end of the buffer; you
  938. can also signal errors explicitly with the functions `error' and
  939. `signal'.
  940.  
  941.    Quitting, which happens when the user types `C-g', is not considered
  942. an error, but it is handled almost like an error.  *Note Quitting::.
  943.  
  944.  - Function: error FORMAT-STRING &rest ARGS
  945.      This function signals an error with an error message constructed by
  946.      applying `format' (*note String Conversion::.) to FORMAT-STRING
  947.      and ARGS.
  948.  
  949.      These examples show typical uses of `error':
  950.  
  951.           (error "You have committed an error.
  952.                   Try something else.")
  953.                error--> You have committed an error.
  954.                   Try something else.
  955.           
  956.           (error "You have committed %d errors." 10)
  957.                error--> You have committed 10 errors.
  958.  
  959.      `error' works by calling `signal' with two arguments: the error
  960.      symbol `error', and a list containing the string returned by
  961.      `format'.
  962.  
  963.      If you want to use your own string as an error message verbatim,
  964.      don't just write `(error STRING)'.  If STRING contains `%', it
  965.      will be interpreted as a format specifier, with undesirable
  966.      results.  Instead, use `(error "%s" STRING)'.
  967.  
  968.  - Function: signal ERROR-SYMBOL DATA
  969.      This function signals an error named by ERROR-SYMBOL.  The
  970.      argument DATA is a list of additional Lisp objects relevant to the
  971.      circumstances of the error.
  972.  
  973.      The argument ERROR-SYMBOL must be an "error symbol"--a symbol
  974.      bearing a property `error-conditions' whose value is a list of
  975.      condition names.  This is how XEmacs Lisp classifies different
  976.      sorts of errors.
  977.  
  978.      The number and significance of the objects in DATA depends on
  979.      ERROR-SYMBOL.  For example, with a `wrong-type-arg' error, there
  980.      are two objects in the list: a predicate that describes the type
  981.      that was expected, and the object that failed to fit that type.
  982.      *Note Error Symbols::, for a description of error symbols.
  983.  
  984.      Both ERROR-SYMBOL and DATA are available to any error handlers
  985.      that handle the error: `condition-case' binds a local variable to
  986.      a list of the form `(ERROR-SYMBOL .  DATA)' (*note Handling
  987.      Errors::.).  If the error is not handled, these two values are
  988.      used in printing the error message.
  989.  
  990.      The function `signal' never returns (though in older Emacs versions
  991.      it could sometimes return).
  992.  
  993.           (signal 'wrong-number-of-arguments '(x y))
  994.                error--> Wrong number of arguments: x, y
  995.  
  996.           (signal 'no-such-error '("My unknown error condition."))
  997.                error--> peculiar error: "My unknown error condition."
  998.  
  999.      Common Lisp note: XEmacs Lisp has nothing like the Common Lisp
  1000.      concept of continuable errors.
  1001.  
  1002. 
  1003. File: lispref.info,  Node: Processing of Errors,  Next: Handling Errors,  Prev: Signaling Errors,  Up: Errors
  1004.  
  1005. How XEmacs Processes Errors
  1006. ...........................
  1007.  
  1008.    When an error is signaled, `signal' searches for an active "handler"
  1009. for the error.  A handler is a sequence of Lisp expressions designated
  1010. to be executed if an error happens in part of the Lisp program.  If the
  1011. error has an applicable handler, the handler is executed, and control
  1012. resumes following the handler.  The handler executes in the environment
  1013. of the `condition-case' that established it; all functions called
  1014. within that `condition-case' have already been exited, and the handler
  1015. cannot return to them.
  1016.  
  1017.    If there is no applicable handler for the error, the current command
  1018. is terminated and control returns to the editor command loop, because
  1019. the command loop has an implicit handler for all kinds of errors.  The
  1020. command loop's handler uses the error symbol and associated data to
  1021. print an error message.
  1022.  
  1023.    An error that has no explicit handler may call the Lisp debugger.
  1024. The debugger is enabled if the variable `debug-on-error' (*note Error
  1025. Debugging::.) is non-`nil'.  Unlike error handlers, the debugger runs
  1026. in the environment of the error, so that you can examine values of
  1027. variables precisely as they were at the time of the error.
  1028.  
  1029. 
  1030. File: lispref.info,  Node: Handling Errors,  Next: Error Symbols,  Prev: Processing of Errors,  Up: Errors
  1031.  
  1032. Writing Code to Handle Errors
  1033. .............................
  1034.  
  1035.    The usual effect of signaling an error is to terminate the command
  1036. that is running and return immediately to the XEmacs editor command
  1037. loop.  You can arrange to trap errors occurring in a part of your
  1038. program by establishing an error handler, with the special form
  1039. `condition-case'.  A simple example looks like this:
  1040.  
  1041.      (condition-case nil
  1042.          (delete-file filename)
  1043.        (error nil))
  1044.  
  1045. This deletes the file named FILENAME, catching any error and returning
  1046. `nil' if an error occurs.
  1047.  
  1048.    The second argument of `condition-case' is called the "protected
  1049. form".  (In the example above, the protected form is a call to
  1050. `delete-file'.)  The error handlers go into effect when this form
  1051. begins execution and are deactivated when this form returns.  They
  1052. remain in effect for all the intervening time.  In particular, they are
  1053. in effect during the execution of functions called by this form, in
  1054. their subroutines, and so on.  This is a good thing, since, strictly
  1055. speaking, errors can be signaled only by Lisp primitives (including
  1056. `signal' and `error') called by the protected form, not by the
  1057. protected form itself.
  1058.  
  1059.    The arguments after the protected form are handlers.  Each handler
  1060. lists one or more "condition names" (which are symbols) to specify
  1061. which errors it will handle.  The error symbol specified when an error
  1062. is signaled also defines a list of condition names.  A handler applies
  1063. to an error if they have any condition names in common.  In the example
  1064. above, there is one handler, and it specifies one condition name,
  1065. `error', which covers all errors.
  1066.  
  1067.    The search for an applicable handler checks all the established
  1068. handlers starting with the most recently established one.  Thus, if two
  1069. nested `condition-case' forms offer to handle the same error, the inner
  1070. of the two will actually handle it.
  1071.  
  1072.    When an error is handled, control returns to the handler.  Before
  1073. this happens, XEmacs unbinds all variable bindings made by binding
  1074. constructs that are being exited and executes the cleanups of all
  1075. `unwind-protect' forms that are exited.  Once control arrives at the
  1076. handler, the body of the handler is executed.
  1077.  
  1078.    After execution of the handler body, execution continues by returning
  1079. from the `condition-case' form.  Because the protected form is exited
  1080. completely before execution of the handler, the handler cannot resume
  1081. execution at the point of the error, nor can it examine variable
  1082. bindings that were made within the protected form.  All it can do is
  1083. clean up and proceed.
  1084.  
  1085.    `condition-case' is often used to trap errors that are predictable,
  1086. such as failure to open a file in a call to `insert-file-contents'.  It
  1087. is also used to trap errors that are totally unpredictable, such as
  1088. when the program evaluates an expression read from the user.
  1089.  
  1090.    Error signaling and handling have some resemblance to `throw' and
  1091. `catch', but they are entirely separate facilities.  An error cannot be
  1092. caught by a `catch', and a `throw' cannot be handled by an error
  1093. handler (though using `throw' when there is no suitable `catch' signals
  1094. an error that can be handled).
  1095.  
  1096.  - Special Form: condition-case VAR PROTECTED-FORM HANDLERS...
  1097.      This special form establishes the error handlers HANDLERS around
  1098.      the execution of PROTECTED-FORM.  If PROTECTED-FORM executes
  1099.      without error, the value it returns becomes the value of the
  1100.      `condition-case' form; in this case, the `condition-case' has no
  1101.      effect.  The `condition-case' form makes a difference when an
  1102.      error occurs during PROTECTED-FORM.
  1103.  
  1104.      Each of the HANDLERS is a list of the form `(CONDITIONS BODY...)'.
  1105.      Here CONDITIONS is an error condition name to be handled, or a
  1106.      list of condition names; BODY is one or more Lisp expressions to
  1107.      be executed when this handler handles an error.  Here are examples
  1108.      of handlers:
  1109.  
  1110.           (error nil)
  1111.           
  1112.           (arith-error (message "Division by zero"))
  1113.           
  1114.           ((arith-error file-error)
  1115.            (message
  1116.             "Either division by zero or failure to open a file"))
  1117.  
  1118.      Each error that occurs has an "error symbol" that describes what
  1119.      kind of error it is.  The `error-conditions' property of this
  1120.      symbol is a list of condition names (*note Error Symbols::.).
  1121.      Emacs searches all the active `condition-case' forms for a handler
  1122.      that specifies one or more of these condition names; the innermost
  1123.      matching `condition-case' handles the error.  Within this
  1124.      `condition-case', the first applicable handler handles the error.
  1125.  
  1126.      After executing the body of the handler, the `condition-case'
  1127.      returns normally, using the value of the last form in the handler
  1128.      body as the overall value.
  1129.  
  1130.      The argument VAR is a variable.  `condition-case' does not bind
  1131.      this variable when executing the PROTECTED-FORM, only when it
  1132.      handles an error.  At that time, it binds VAR locally to a list of
  1133.      the form `(ERROR-SYMBOL . DATA)', giving the particulars of the
  1134.      error.  The handler can refer to this list to decide what to do.
  1135.      For example, if the error is for failure opening a file, the file
  1136.      name is the second element of DATA--the third element of VAR.
  1137.  
  1138.      If VAR is `nil', that means no variable is bound.  Then the error
  1139.      symbol and associated data are not available to the handler.
  1140.  
  1141.    Here is an example of using `condition-case' to handle the error
  1142. that results from dividing by zero.  The handler prints out a warning
  1143. message and returns a very large number.
  1144.  
  1145.      (defun safe-divide (dividend divisor)
  1146.        (condition-case err
  1147.            ;; Protected form.
  1148.            (/ dividend divisor)
  1149.          ;; The handler.
  1150.          (arith-error                        ; Condition.
  1151.           (princ (format "Arithmetic error: %s" err))
  1152.           1000000)))
  1153.      => safe-divide
  1154.  
  1155.      (safe-divide 5 0)
  1156.           -| Arithmetic error: (arith-error)
  1157.      => 1000000
  1158.  
  1159. The handler specifies condition name `arith-error' so that it will
  1160. handle only division-by-zero errors.  Other kinds of errors will not be
  1161. handled, at least not by this `condition-case'.  Thus,
  1162.  
  1163.      (safe-divide nil 3)
  1164.           error--> Wrong type argument: integer-or-marker-p, nil
  1165.  
  1166.    Here is a `condition-case' that catches all kinds of errors,
  1167. including those signaled with `error':
  1168.  
  1169.      (setq baz 34)
  1170.           => 34
  1171.  
  1172.      (condition-case err
  1173.          (if (eq baz 35)
  1174.              t
  1175.            ;; This is a call to the function `error'.
  1176.            (error "Rats!  The variable %s was %s, not 35" 'baz baz))
  1177.        ;; This is the handler; it is not a form.
  1178.        (error (princ (format "The error was: %s" err))
  1179.               2))
  1180.      -| The error was: (error "Rats!  The variable baz was 34, not 35")
  1181.      => 2
  1182.  
  1183. 
  1184. File: lispref.info,  Node: Error Symbols,  Prev: Handling Errors,  Up: Errors
  1185.  
  1186. Error Symbols and Condition Names
  1187. .................................
  1188.  
  1189.    When you signal an error, you specify an "error symbol" to specify
  1190. the kind of error you have in mind.  Each error has one and only one
  1191. error symbol to categorize it.  This is the finest classification of
  1192. errors defined by the XEmacs Lisp language.
  1193.  
  1194.    These narrow classifications are grouped into a hierarchy of wider
  1195. classes called "error conditions", identified by "condition names".
  1196. The narrowest such classes belong to the error symbols themselves: each
  1197. error symbol is also a condition name.  There are also condition names
  1198. for more extensive classes, up to the condition name `error' which
  1199. takes in all kinds of errors.  Thus, each error has one or more
  1200. condition names: `error', the error symbol if that is distinct from
  1201. `error', and perhaps some intermediate classifications.
  1202.  
  1203.    In order for a symbol to be an error symbol, it must have an
  1204. `error-conditions' property which gives a list of condition names.
  1205. This list defines the conditions that this kind of error belongs to.
  1206. (The error symbol itself, and the symbol `error', should always be
  1207. members of this list.)  Thus, the hierarchy of condition names is
  1208. defined by the `error-conditions' properties of the error symbols.
  1209.  
  1210.    In addition to the `error-conditions' list, the error symbol should
  1211. have an `error-message' property whose value is a string to be printed
  1212. when that error is signaled but not handled.  If the `error-message'
  1213. property exists, but is not a string, the error message `peculiar
  1214. error' is used.
  1215.  
  1216.    Here is how we define a new error symbol, `new-error':
  1217.  
  1218.      (put 'new-error
  1219.           'error-conditions
  1220.           '(error my-own-errors new-error))
  1221.      => (error my-own-errors new-error)
  1222.      (put 'new-error 'error-message "A new error")
  1223.      => "A new error"
  1224.  
  1225. This error has three condition names: `new-error', the narrowest
  1226. classification; `my-own-errors', which we imagine is a wider
  1227. classification; and `error', which is the widest of all.
  1228.  
  1229.    The error string should start with a capital letter but it should
  1230. not end with a period.  This is for consistency with the rest of Emacs.
  1231.  
  1232.    Naturally, XEmacs will never signal `new-error' on its own; only an
  1233. explicit call to `signal' (*note Signaling Errors::.) in your code can
  1234. do this:
  1235.  
  1236.      (signal 'new-error '(x y))
  1237.           error--> A new error: x, y
  1238.  
  1239.    This error can be handled through any of the three condition names.
  1240. This example handles `new-error' and any other errors in the class
  1241. `my-own-errors':
  1242.  
  1243.      (condition-case foo
  1244.          (bar nil t)
  1245.        (my-own-errors nil))
  1246.  
  1247.    The significant way that errors are classified is by their condition
  1248. names--the names used to match errors with handlers.  An error symbol
  1249. serves only as a convenient way to specify the intended error message
  1250. and list of condition names.  It would be cumbersome to give `signal' a
  1251. list of condition names rather than one error symbol.
  1252.  
  1253.    By contrast, using only error symbols without condition names would
  1254. seriously decrease the power of `condition-case'.  Condition names make
  1255. it possible to categorize errors at various levels of generality when
  1256. you write an error handler.  Using error symbols alone would eliminate
  1257. all but the narrowest level of classification.
  1258.  
  1259.    *Note Standard Errors::, for a list of all the standard error symbols
  1260. and their conditions.
  1261.  
  1262.